import axios from "axios";
import req from "@/config/host";

axios.interceptors.request.use(
  config => {
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

axios.interceptors.response.use(
  res => {
    return res;
  },
  err => {
    return Promise.reject(err);
  }
);

axios.defaults.timeout = 5000; //间隔时间
let HeaderCont = "application/json; charset=utf-8";

export let Ajax = (type, url, data = {}, headersV = HeaderCont) => {
  return new Promise((resolve, reject) => {
    let init = {
      mathod: type,
      url: "",
      headers: {
        "Content-Type": headersV
      }
    };

    if (!/^(http|https):\/\/+/g.test(url)) init.baseURL = req;

    type === "get" ? (init["params"] = data) : (init["data"] = data);

    axios(init)
      .then(
        data => {
          return resolve(data);
        },
        err => {
          reject(err);
        }
      )
      .catch(err => {
        reject(err);
      });
  });
};
